﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MakeIT_Bill_Maker
{
    public partial class frmAddPayment : Form
    {
        // Data Connection
        private Database.DatabaseConnection db = new Database.DatabaseConnection();
        private DataSet ds;
        private string sql;
        private DataTable dt;

        public frmAddPayment()
        {
            InitializeComponent();
        }

        // Form Load
        private void frmAddPayment_Load(object sender, EventArgs e)
        {
            sql = "SELECT AptNo FROM Users";
            ds = db.Show(sql);
            dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                cmbAptNo.Items.Add(row[0]);
            }
        }

        // When the text in 'cmbAptNo' changes
        private void cmbAptNo_TextChanged(object sender, EventArgs e)
        {
            string AptNo = cmbAptNo.Text;

            sql = "SELECT AptNo, Name, MeterNo FROM Users WHERE AptNo='" + AptNo + "'";
            ds = db.Show(sql);
            dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                txtName.Text = row[1].ToString();
                txtMeterNo.Text = row[2].ToString();
            }

            txtPayment.Clear();
        }

        // 'Close' button
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // 'Next' button
        private void btnNext_Click(object sender, EventArgs e)
        {
            clear_All();
        }

        // 'Add' button
        private void btnAdd_Click(object sender, EventArgs e)
        {
            double out1, prevArrears=0, monthlyArrears=0, totVal=0, totMonthlyArrears=0;
            string payment = txtPayment.Text;
            string aptNo = cmbAptNo.Text;

            if (!double.TryParse(payment, out out1))
            {
                MessageBox.Show("'Payment' must be a currency value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                sql = "SELECT Arrears, MonthlyArrears FROM Users WHERE AptNo='" + aptNo + "'";
                ds = db.Show(sql);
                dt = ds.Tables[0];
                foreach (DataRow row in dt.Rows)
                {
                    if (!double.TryParse(row[0].ToString(), out out1))
                    {
                        MessageBox.Show("No 'Arrears' assigned for this apartment.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    if (!double.TryParse(row[1].ToString(), out out1))
                    {
                        MessageBox.Show("No 'Monthly Arrears' assigned for this apartment.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        prevArrears = double.Parse(row[0].ToString());
                        monthlyArrears = double.Parse(row[1].ToString());
                    }
                }

                totVal = prevArrears - double.Parse(payment.ToString());
                totMonthlyArrears = monthlyArrears - double.Parse(payment.ToString());

                sql = "UPDATE Users SET Arrears = '" + totVal + "', MonthlyArrears = '" + totMonthlyArrears + "' WHERE AptNo='" + aptNo + "'";
                bool b = db.edit(sql);

                string sql1 = "SELECT TotalPayments FROM OtherFixed WHERE Index=1";
                DataSet dataset1 = db.Show(sql1);
                DataTable dt1 = dataset1.Tables[0];
                double totalPayments = double.Parse(dt1.Rows[0]["TotalPayments"].ToString());
                totalPayments += double.Parse(payment.ToString());
                sql1 = "UPDATE OtherFixed SET TotalPayments = '" + totalPayments + "' WHERE Index=1";
                bool c = db.edit(sql1);

                if (b == true)
                {
                    MessageBox.Show("Value updated succesfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Failed to update the value.", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // Method to clear the window 
        private void clear_All()
        {
            cmbAptNo.Text = "";
            txtName.Clear();
            txtMeterNo.Clear();
            txtPayment.Clear();
        }
    }
}
